<?php

namespace app\index\service;

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Clients\StsClient;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

/**
 * 短信业务扩展服务
 * Class ExtendService
 * @package app\store\service
 */
class ExtendService
{

    /**
     * 阿里云发送短信方法
     * @param $phone
     * @param $code
     * @return array|bool
     * @throws ClientException
     */
    public static function sendAliSms($phone, $code)
    {
        AlibabaCloud::accessKeyClient( 'LTAI5t9nZffQgWkFmJ8dHwaf', 'c2Qoxr4EDbhCpkPX2zNwwh93c6TRWf')
            ->regionId('cn-hangzhou')
            ->asDefaultClient();
        try {
            $result = AlibabaCloud::rpc()
                ->product('Dysmsapi')
                // ->scheme('https') // https | http
                ->version('2017-05-25')
                ->action('SendSms')
                ->method('POST')
                ->host('dysmsapi.aliyuncs.com')
                ->options([
                    'query' => [
                        'PhoneNumbers' => $phone,
                        'SignName' => "杭州网约车助手",
                        'TemplateCode' => "SMS_242910932",
                        'TemplateParam' => json_encode(['code'=>$code]),
                    ],
                ])
                ->request();
            return $result->toArray();
        } catch (ClientException $e) {
            return false;
        } catch (ServerException $e) {
            return false;
        }
    }


    /**
     * 阿里云云语音合成生成token
     * @param $accesskeyid
     * @param $accessKeysecret
     * @return mixed
     */
    public function getalyuntoken($accesskeyid,$accessKeysecret){
        date_default_timezone_set("Etc/GMT");
        $one = md5(time());
        $parms = array(
            "AccessKeyId" => $accesskeyid,
            "Action" => "CreateToken",
            "Version" => "2019-02-28",
            "Format" => "JSON",
            "RegionId" => "cn-shanghai",
            "Timestamp" => date("Y-m-d\TH:i:s\Z"),
            "SignatureMethod" => "HMAC-SHA1",
            "SignatureVersion" => "1.0",
            "SignatureNonce" => substr($one,0,8)."-".substr($one,8,4)."-".substr($one,12,4)."-".substr($one,16,4)."-".substr($one,20,4),

        );
        ksort($parms);
        $query = http_build_query($parms);
        $stringToSign = "GET&%2F&" . urlencode($query);
        $signature = urlencode(base64_encode(hash_hmac("sha1",$stringToSign,$accessKeysecret."&",true)));
        $queryStringWithSign = "Signature=". $signature ."&". $query;
        $tokenUrl = "http://nls-meta.cn-shanghai.aliyuncs.com/?".$queryStringWithSign;
        $token_string = $this->urlOpen($tokenUrl);
        $token_object = json_decode($token_string,true);
        return $token_object;

    }


    /**
     * 请求
     * @param $url
     * @param null $data
     * @param null $header
     * @return mixed
     */
    public function urlOpen($url,$data = null,$header =null){
        $ch =curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        //curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
        //curl_setopt($ch,CURLOPT_USERAGENT,$url,$ua);
        if($data){
            curl_setopt($ch,CURLOPT_POST,1);
            curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
        }
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_REFERER,$url);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
        $content = curl_exec($ch);
        $error = curl_error($ch);
        if($error){
            die($error);
        }
        curl_close($ch);
        return $content;

    }



}
